var wxUtil = require('../../../utils/wxUtil.js');
var util = require('../../../utils/util.js');
var ajax = require('../../../utils/ajax.js');
var common = require('../../../utils/common.js');
var PageBase = require('../../../common/PageBase.js');
var WxParse = require('../../../utils/wxParse/wxParse.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    dataLoaded: false,
    myHidden: true,
    modalHidden: true,
    addrList: [],
    addressId: '',
    name: '',
    tel: '',
    addr: '',
    province: '',
    city: '',
    county: '',
    provinceId: '',
    cityId: '',
    countyId: '',
  },
  onLoad: function (options) {
    common.initApp({
      loginSuc: () => {
        this.setData({
          isLogin: !!app.getUid(),
        });
        this._initAddr();
      }
    });
  },
  onShow: function () {
  },
  /**
 * 初始化地址列表
 */
  _initAddr: function (isOnload) {
    var _self = this;
    ajax.G({
      url: 'm=user&c=address',
      data: {
        uid: app.getUid(),
        size: 999
      },
      success: (res) => {

        var d = res.data;
        if (d.item) {
          _self.setData({
            addrList: d.item
          })
        }
        var addrList = _self.data.addrList;
        // addrList.some(function (item) {
        //   if (item.isDefault == 1) {
        //     item.selected = true;
        //   }
        // })
        _self.setData({
          addrList,
          dataLoaded: true
        })

      }
    });

  },

  /**
   * 选择地址
   */
  _select: function (e) {
    var _self = this;
    var index = parseInt(e.currentTarget.dataset.index);
    var addrList = this.data.addrList;
    addrList.forEach(function (item, index) {
      if (addrList[index].selected) {
        addrList[index].selected = false;
        _self.setData({
          addrList,
        })
      }
    })
    addrList[index].selected = true;


    _self.setData({
      addressId: addrList[index].id,
      addrList,
    })
    var formId = e.detail.formId;

    //common.submitForm(formId);

    _self._goPrevPage();
  },
  /**
   * 页面跳转
   */
  _jump: function (evt) {
    common.jump(evt);
  },
  /**
   * 设置上一个页面的数据,并跳转到上个页面
   */
  _goPrevPage: function () {
    var _self = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      optionAddrId: _self.data.addressId
    })
    wx.navigateBack({
      delta: 1
    });
  }
}))